<?php
	error_reporting(E_ALL || ~E_NOTICE);
	$i=1;  
	$sqlstr='';
	$gatetab='';
	$dbResult=array();
    $data=file_get_contents('php://input');
//	file_put_contents("test.log","\n".$data."\n",FILE_APPEND);
	$jsondata=json_decode($data,true);	
//	echo $jsondata["ControlerNum"];
//	echo $jsondata["gatenum"];
//	print_r($jsondata);
	$controlerNum=$jsondata["ControlerNum"];
	$gatenum=$jsondata["gatenum"];
//	file_put_contents("test.log","controlerNum: ".$controlerNum."gatenum: ".$gatenum, FILE_APPEND);
    $myconn=@mysql_connect("localhost","root","root");
    if(!$myconn)
    {
        $string = "Can't connect database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
	    die('Could not connect: '.mysql_error());
		return;
    }
    $db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {        
	    die("Can't use database dwaccessDB : ".mysql_error());
        $string = "Can't use database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
		mysql_close($myconn);
		return;
	}
	if($gatenum==="all")
	{
		$strSql="select gatename,region,controlmethod,delayopentime,delayclosetime,sensorstat,attendancecheck,antipassback from gatesetting WHERE controlerNum= '$controlerNum' AND gatenum='0';";
	}else
	{
		$strSql="select gatename,region,controlmethod,delayopentime,delayclosetime,sensorstat,attendancecheck,antipassback from gatesetting WHERE controlerNum= '$controlerNum' AND gatenum='$gatenum';";
	}
	$result=mysql_query($strSql,$myconn);	        
//    @file_put_contents("test.log", "\n strSql =".$strSql." \ndata:".$data." controlerNum:".$controlerNum."\n", FILE_APPEND);
	while($dbResult = mysql_fetch_array($result))
	{	        		   
		for($j=0;$j<8;$j++)
		{			   
			if($i==1 && $j==0)
		    {			        
				$gatetab = "{\"s".$i.'_'.$j."\":\"".$dbResult[$j].'"';						
		    }else
		    {
		        $gatetab = $gatetab.",\"s".$i.'_'.$j."\":\"".$dbResult[$j].'"';
		    }
		}
        $i++;	
//			@file_put_contents("test.log", "\n i =".$i." gatetab =".$dbResult[$i-1]."\n", FILE_APPEND);				
	}
	if($i===1)
	{
	    for($j=0;$j<8;$j++)
		{
		    if($j==0)
			{  
			    $gatetab = "{\"s1_".$j."\":\" \"";	
		    }else
			{
			    $gatetab = $gatetab.",\"s1_".$j."\":\" \"";
			}
		}
	}	
    $gatetab = $gatetab.",\"nrows\":\"".($i-1)."\",\"ncols\":\"8\"}";
	
	mysql_free_result($result);
	mysql_close($myconn);	
	echo $gatetab;
//	    @file_put_contents("test.log", "\n gatetab =".$gatetab."\n", FILE_APPEND);
			
	exit(0);    
?>